--Function of creating boolean column
create or replace procedure meta_add_bool_column(
    --Table id
    a_table_id raw,
    --Column id
    a_column_id raw,
    --Column name
    a_column_name varchar2)
as
    --Table name
    a_table_name varchar2(26);
begin
    p_check_col_not_exists(a_table_id, a_column_id, a_column_name);
         
    a_table_name :=  p_get_table_name(a_table_id);
         
    --Changing the metadata
    insert into meta_bool_columns(column_id, table_id, column_name,
        order_index, customization_level)
    values (a_column_id, a_table_id, a_column_name,
    p_generate_order_index(a_table_id), 1);
         
    --Dropping table view
    p_drop_view_if_exists(a_table_name);
         
    execute immediate 'alter table tbl_' 
    || a_table_name
    || ' add  ' || a_column_name
    || ' number(1)';
    
    --Creating value constraint
    execute immediate 'alter table tbl_'
    || a_table_name
    || ' add constraint b' || substr(a_column_id, 1, 29)
    || ' check(' || a_column_name || '=0 or ' 
    || a_column_name || '=1)';
         
    --Creating table view
    p_create_table_view(a_table_id);

end;

--go

--Function of setting column default value
create or replace procedure meta_set_bool_default(
    --Column id
    a_column_id raw,
    --Default value
    a_default_value number)
as
    --Table id
    a_table_id raw(16);
    --Table name
    a_table_name varchar2(26);
    --Column name
    a_column_name varchar2(26);
    --Customization level
    a_customization_level number(10);
begin
    p_check_col_type(a_column_id, 'meta_bool_columns', 'bool');
    
    --Reading column properties
    select table_id, column_name, customization_level
    into a_table_id, a_column_name, a_customization_level
    from meta_bool_columns
    where column_id = a_column_id;
    
    a_table_name := p_get_table_name(a_table_id);
    
    --If this is a system column
    if a_customization_level = 0 then
        raise_application_error(-20008,
                'Changing bool system column default value is forbidden (column_id = '
                || a_column_id
                || ') (#error_code = 20008)');
    end if;
    
    if a_default_value is not null then
        execute immediate 'alter table tbl_' 
        || a_table_name
        || ' modify (' || a_column_name
        || ' default ' || a_default_value || ')';
    else
        execute immediate 'alter table tbl_' 
        || a_table_name
        || ' modify (' || a_column_name
        || ' default null)';
    end if;
    
    --Updating the metadata
    update meta_bool_columns
    set default_value = a_default_value
    where column_id = a_column_id;
end;

--go